package com.example.kam.mymediapalyer;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.kam.mymediapalyer.util.Player;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;
import java.util.regex.Pattern;

import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;

/**
 * Created by kam on 2017/2/12.
 */

public class ListViewAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private List<SmbFile> mCurrentFiles = new ArrayList<>();
    private Stack<List<SmbFile>> mStackFiles;
    private Context mContext;

    ListViewAdapter(Context context) {
        mStackFiles = new Stack<>();
        mInflater = LayoutInflater.from(context);
        mContext = context;
    }

    public void setCurrentFiles(List<SmbFile> currentFiles) {
        this.mCurrentFiles = currentFiles;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mCurrentFiles.size();
    }

    @Override
    public SmbFile getItem(int position) {
        return mCurrentFiles.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder();
            /**得到各个控件的对象*/
            holder.imageView = (ImageView) convertView.findViewById(R.id.image_view);
            holder.title = (TextView) convertView.findViewById(R.id.title);
            holder.item = convertView.findViewById(R.id.item);
            convertView.setTag(holder);//绑定ViewHolder对象
        } else {
            holder = (ViewHolder) convertView.getTag();//取出ViewHolder对象
        }
        final SmbFile file = getItem(position);
        /**设置TextView显示的内容，即我们存放在动态数组中的数据*/
        holder.title.setText(file.getName());
        try {
            if (file.isDirectory()) {
                holder.imageView.setImageResource(R.mipmap.ic_folder);
                holder.item.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        try {
                            mStackFiles.push(mCurrentFiles);
                            setCurrentFiles(Arrays.asList(file.listFiles()));
                        } catch (SmbException e) {
                            e.printStackTrace();
                        }
                    }
                });
            } else {
                holder.imageView.setImageURI(null);
                holder.item.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (isShiPing(file.getName())) {
                            Player.play(mContext, file);
                        } else {
                            Toast.makeText(mContext, "不是视屏", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        } catch (SmbException e) {
            e.printStackTrace();
        }
        return convertView;
    }

    public boolean isShiPing(String name) {
        String reg = "(mp4|flv|avi|rm|rmvb|wmv|mkv)";
        Pattern p = Pattern.compile(reg);
        return p.matcher(name).find();
    }

    /**
     * 存放控件
     */
    public final class ViewHolder {
        public ImageView imageView;
        public TextView title;
        public View item;
    }

    public boolean isRoot() {
        if (mCurrentFiles.size() == 0 || mCurrentFiles.get(0) == null) {
            return false;
        }
        return "IPC$/".equals(mCurrentFiles.get(0).getName());
    }

    public void goToPre() {
        setCurrentFiles(mStackFiles.pop());
    }

    public void saveData() {
        for (SmbFile mCurrentFile : mCurrentFiles) {
            if (isShiPing(mCurrentFile.getName())) {
                DataHolder.files.add(mCurrentFile);
            }
        }
    }

    public void onKeyGo(List<SmbFile> smbFiles) {
        mStackFiles.push(mCurrentFiles);
        setCurrentFiles(smbFiles);
    }

}
